import { useEffect, useRef, useState } from 'react'
export default function useCountDown(initCount=10,callBack=()=>{}){
    const timeId = useRef<{id:number}>({ id: 0 })
    const [count, setCount] = useState(initCount)
    const start =()=>{
        setCount(initCount)
        timeId.current.id=window.setInterval(()=>{
            setCount((count)=>count-1)
        },1000)
    }
    useEffect(()=>
    window.clearInterval(timeId.current.id)
    ,[])
    useEffect(()=>{
      if(count===0){
          clearInterval(timeId.current.id)
          callBack()
      }
    },[callBack, count])
    return {start,count}
}